Laufzeitinformation über interne Tabelle

Zur Laufzeit Informationen über im Report deklarierte Objekte (Strukturen, Tabellen, etc.) zu bekommen ist mithilfe der RTTI-Klassen (RTTI = Run Time Type Information) recht einfach. Man muss nur wissen, dass es sie gibt…

Mit dem folgenden Programm werden die Langtexte der im Datenelement hinterlegten Felddefinitionen einer internen Struktur ermittelt.

Ausgabe

Das Programm erzeugt eine Liste mit allen in der Struktur “WA_TAB” vorhandenen Felder mit der Information, ob das Feld einen Bezug zum Data Dictionary hat und falls ja: den Langtext zum Datenelement.

Info | Feld | Beschreibung des Feldes

Coding

REPORT.
*== Types
TYPES: BEGIN OF ty_test,
         langu TYPE sylangu,
         abcde TYPE syabcde,
       END OF ty_test.

*== data
DATA: BEGIN OF wa_tab,
        kunnr  LIKE knvv-kunnr,
        matnr  LIKE mara-matnr,
        maktx  LIKE makt-maktx,
        kbetr  LIKE konp-kbetr,
        flag   TYPE c LENGTH 10, " Ohne DDIC
        werks  TYPE werks_d,     " Datenelement
        test   TYPE ty_test,     " Struktur!!
      END OF wa_tab.

START-OF-SELECTION.
*== explore structure:
  PERFORM show_struc_info.
*&---------------------------------------------------------------------*
*&      Form  struc
*&---------------------------------------------------------------------*
FORM struc USING value(istruc).
  DATA  cl_type               TYPE REF TO cl_abap_typedescr.
  DATA  cl_struc              TYPE REF TO cl_abap_structdescr.
  DATA  cl_elem               TYPE REF TO cl_abap_elemdescr.
  FIELD-SYMBOLS <fs_comps>    TYPE abap_compdescr.
  DATA  l_type_kind           TYPE abap_typekind.
  DATA  l_name                TYPE string.
  FIELD-SYMBOLS <fs_any>      TYPE ANY.
  DATA wa_dfies               TYPE dfies.

  cl_struc ?= cl_abap_datadescr=>describe_by_data( p_data = istruc  ). 
*== check each element of given structure 
  LOOP AT cl_struc->components ASSIGNING <fs_comps>. 
    ASSIGN COMPONENT <fs_comps>-name OF STRUCTURE istruc TO <fs_any>. 
    IF sy-subrc EQ 0. 
*== Describe field 
      DESCRIBE FIELD <fs_any> TYPE l_type_kind. 
      IF l_type_kind CA 'uv'. 
*== again a structure... 
        PERFORM struc USING <fs_any>. 
      ELSE.
*== Element: get definition 
        cl_elem ?= cl_abap_datadescr=>describe_by_data( p_data = <fs_any>  ). 
*== Name of field 
        l_name = cl_elem->get_relative_name( ). 
*== if referenced to dictionary 
        IF cl_elem->is_ddic_type( ) = 'X'. 
          TRY. 
*== get information from dictionary 
            wa_dfies = cl_elem->get_ddic_field( ). 
            WRITE : / 'DDIC:', 12 <fs_comps>-name, wa_dfies-scrtext_l. 
          CATCH cx_root. 
            CLEAR wa_dfies. 
            WRITE : / 'Error DDIC:', 12 <fs_comps>-name, l_name. 
          ENDTRY. 
        ELSE. 
*== If no dictionary reference: use elements name 
          WRITE : / 'No DDIC:', 12 <fs_comps>-name, l_name. 
        ENDIF. 
      ENDIF. 
    ENDIF. 
  ENDLOOP.

ENDFORM.                    "struc 
*&---------------------------------------------------------------------* 
*&      Form  show_struc_info 
*&---------------------------------------------------------------------* 
FORM show_struc_info.
  DATA  l_type_kind           TYPE abap_typekind. 
*== Which kind of data? 
  l_type_kind = cl_abap_datadescr=>get_data_type_kind( p_data = wa_tab ). 
*== extract structures 
  IF l_type_kind CA 'uv'. 
    PERFORM struc USING wa_tab. 
  ENDIF. 

ENDFORM.                    " test_struct
Enno Wulff
Letzte Artikel von Enno Wulff (Alle anzeigen)